<?php


namespace app\common\order\model;


use think\Model;
use think\model\concern\SoftDelete;

class OrderCollect extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
        'collect_date' => 'timestamp',
        'check_time' => 'timestamp',
    ];

    public function order() {
        return $this->hasOne(Order::class, 'id', 'order_id');
    }

    public function getIncomeAttr($value, $data) {
        $where = [];
        $where[] = ['order_id', '=', $data['order_id']];
        $where[] = ['income_type_id', '=', $data['type']];
        $model = new OrderIncome();
        $row = $model->where($where)->find();
        return $row ? $row->append(['way', 'type', 'user', 'check_tag']) : [
            'way' => [
                'id' => '',
                'title' => ''
            ],
            'type' => [
                'id' => '',
                'title' => ''
            ],
            'user' => [
                'id' => '',
                'realname' => '',
                'status' => ''
            ],
            'check_tag' => '',
            'total' => '',
            'income_date' => ''
        ];
    }

    public function getTitleAttr($value, $data) {
        $arr = [
            1 => '定金',
            2 => '中款',
            3 => '尾款'
        ];
        $index = $data['type'];

        return $arr[$index];
    }

    public function getCollectDateAttr($value, $data) {
        return date('Y-m-d', $value);
    }

    public function getCheckTagAttr($value, $data) {
        $index = $data['check_status'];
        $map = [
            -1 => '暂存',
            0 => '待审核',
            1 => '审核中',
            2 => '审核通过',
            3 => '审核驳回'
        ];

        return $map[$index];
    }
}